package core;

import java.io.File;

import com.thoughtworks.selenium.*;
import static core.SeleneseTestCaseEx.*;

public class Action {
	private static HandlingError err = HandlingError.getInstance();
	
	public void launchBrowser() {
		repEng.newAction("Launch '" + SeleneseTestCaseEx.getBrowser() + "' browser.");
		
		try {
			BrowserConfigurationOptions opt = new BrowserConfigurationOptions();
    		opt.setTimeoutInSeconds(60);
			
			browser.start(opt);
			browser.setTimeout(tc.getEnv("waitpageload"));
			browser.windowMaximize();
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}
	}
	
	public void openURL(String url) {
		repEng.newAction("Navigate to '" + url + "' url");
		try {
			browser.open(url);
			browser.waitForPageToLoad(tc.getEnv("waitpageload"));
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}		
		
	}
	
	public void click(String locator) {
		repEng.newAction("Click '" + locator + "' web element");
		try {
			browser.click(locator);
			browser.waitForPageToLoad(tc.getEnv("waitpageload"));
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}
	}
	
	public void clickNoWait(String locator) {
		repEng.newAction("Click '" + locator + "' web element without sync");
		try {
			browser.click(locator);			
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}
	}
	
	public void typeByContainsId(String textId, String textForTyping) {
		repEng.newAction("Type '" + textForTyping + "' text to '" + textId + "' web element");
		try {
			browser.type("//*[contains(@id, '" + textId + "')]", textForTyping);
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}
		
	}
	
	public void navigateToEmail(String url, String emailName, String password) {
		repEng.newAction("Navigate to email (url='" + url  + "'; username='" + emailName + "'; password='" + password + "').");
		
		try {
			browser.open(url);
			browser.waitForPageToLoad(tc.getEnv("waitpageload"));
			browser.type("//*[contains(@id, 'some id')]", emailName); //TODO needs for change
			browser.type("//*[contains(@id, 'some id')]", password); //TODO needs for change
			browser.click("locator");//TODO needs for change
			browser.waitForPageToLoad(tc.getEnv("waitpageload"));
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}
	}
	
	public void setComboBox(String locator, String text) {
		repEng.newAction("Select '" + text + "' item from combobox ('" + locator + "')");
		
		try {
			browser.type(locator, "");
			browser.typeKeys(locator, text);
			browser.fireEvent(locator, "blur");
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}
	}
	
	public void setDate(String locator, String text) {
		repEng.newAction("Select '" + text + "' to date field ");
		try {
			browser.type(locator, text);
	        browser.fireEvent(locator, "blur");
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}		
	}
	
	/*public void setFile(String locator, String path) {
		repEng.newAction("upload '" + path + "' file to site. Locator:'" + locator + "'");
		try {
			browser.type(locator, path);			
			browser.waitForCondition("selenium.isElementPresent('link=Clear')", tc.getEnv("waitpageload"));
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}		
	}	*/
	
	
	public void setFile(String locator, String fileName) {
		//repEng.newAction("Upload '" + fileName + "' file to site. Locator:'" + locator + "'");
		tc.getEnv("_testname");
		String path = getPathToRes() + "\\" + tc.getEnv("_testname") + "\\" + fileName;
		//String path = "/SeleniumPrj/res/CreateBrandTest/" + fileName;
		repEng.newAction("Upload   " + path + "   file to site. Locator:'" + locator + "'");
		try {
			browser.type(locator, path);			
			browser.waitForCondition("selenium.isElementPresent('link=Clear')", tc.getEnv("waitpageload"));
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}		
	}	
	
	public void setFileNoWait(String locator, String fileName) {
		//repEng.newAction("Upload '" + fileName + "' file to site. Locator:'" + locator + "'");
		tc.getEnv("_testname");
		String path = getPathToRes() + "\\" + tc.getEnv("_testname") + "\\" + fileName;
		//String path = "/SeleniumPrj/res/CreateBrandTest/" + fileName;
		repEng.newAction("Upload   " + path + "   file to site. Locator:'" + locator + "'");
		try {
			browser.type(locator, path);			
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}		
	}
	
	public void difficultToImplement(String text) {
		repEng.newAction(text);
		try {
			err.setWarnDesc("This item is difficult for implementation!");
			err.checkResult();
		} catch (Exception e){
		}
	}
	
	private String getPathToRes() {			
		String absolutePath;
	    File file;
	     
		if (SeleneseTestCaseEx.getServerName().toLowerCase().contentEquals("localhost")) {
			file = new File("res");
			absolutePath = file.getAbsolutePath();
			return absolutePath;
			} else {	
				return null;
			 //TODO Need to change
			}
		//return "./res";
	}
	
   public void waitForElementVisible(String locator, String timeout) {
	   Integer timesec = Integer.parseInt(timeout)*60;
	   repEng.newAction("Waiting for element ('" + locator + "') to visible on page for '" + timesec.toString() + "'  sec.");
		try {
			browser.waitForElementVisible(locator, timeout);
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}
   }
   
   public void waitForElementNotVisible(String locator, String timeout) {
	   Integer timesec = Integer.parseInt(timeout)*60;
	   repEng.newAction("Waiting for element ('" + locator + "') to not visible on page for '" + timesec.toString() + "'  sec.");
		try {
			browser.waitForElementNotVisible(locator, timeout);
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}
   }
  
   public void sleep(int time) {
	   repEng.newAction("Waiting for ('" + time + "') miliseconds");		
	   try {
		   Thread.sleep(time);	
		} catch (Exception e) {
			err.setErrDesc(e);
		} finally {
			err.checkResult();
		}		
   }

}
